'use strict';

app.controller('OrderController', ['$scope','$modal','dbUtils','OrderService',function($scope,$modal,dbUtils,orderService) {


    $scope.$on('sendOrder',function(event,orderNo){
        orderService.send(orderNo).then(function(resp){
            $scope.dbFormGrid.reLoadData();
        });

    });
    $scope.$on('receiveOrder',function(event,orderNo){
        orderService.receive(orderNo).then(function(resp){
            $scope.dbFormGrid.reLoadData();
        });
    });
    $scope.$on('showOrder',function(event,orderNo){
        var instance = $modal.open({
            animation: true,
            templateUrl: 'tpl/order/orderDetail.html',
            controller: 'OrderDetailController',
            size: "lg",
            backdrop: "static",
            resolve: {
                source: function () {
                    return {orderNo:orderNo}
                }
            }
        });
        instance.result.then(function () {
            $scope.dbFormGrid.reLoadData();
        });
    });

    var formGridOptions = {
        form: {
            settings: {
                cols: 3
            },
            fields: [
                {name: "orderNo", label: "订单号", type: "text",labelCols: "2"},
                {
                    name: "orderStatus",
                    label: "订单状态",
                    labelCols: "2",
                    type: "select",
                    items:[{
                        "key":"1",
                        "value":"待发货"
                    },{
                        "key":"2",
                        "value":"已发货"
                    },{
                        "key":"3",
                        "value":"已收货"
                    },{
                        "key":"4",
                        "value":"已完成"
                    }]
                },
                {name: "_orderDateRange","label": "日期", "type": "dateRange", "labelCols": "2"}
            ]
        },
        grid: {
            settings: {
                transCode: "/orders/list",
                autoLoad: true,
                page: {pageSize: 10},
                showCheckBox: true
            },
            header: [
                {name: "订单编号", field: "orderNo"},
                {name: "下单时间", field: "createTimeStr"},
                {name: "当前状态", field: "statusDesc"},
                {name: "商品金额", field: "totalPrice"},
                {name: "配送地址", field: "address"},
                {name: "联系电话", field: "phone"},
                {name: "操作", field: "operation",compile:true}
            ],
            rowOperation: {show: false}
        }
    }

    var formGridEvents = {
        grid: {
            fieldEvents: {
                "partyNoClick":function(row){
                	openModal(row);
                },
                operationFormat:function(value,row){
                    //publishevent
                    var html = "<button type=\"button\" style=\"padding: 3px 8px; margin-right:3px;\" class=\"btn btn-default btn-success\" ng-click=\"publishEvent('showOrder','"+row.orderNo+"')\">订单详情</button>";
                    if(row.status == 1 ){
                        html+= "<button type=\"button\" style=\"padding: 3px 8px; margin-right:3px;\" class=\"btn btn-default btn-danger\" ng-click=\"publishEvent('sendOrder','"+row.orderNo+"')\">已发货</button>";
                    }
                    if(row.status == 2){
                        html+="<button type=\"button\" style=\"padding: 3px 8px; margin-right:3px;\" class=\"btn btn-default btn-danger\" ng-click=\"publishEvent('receiveOrder','"+row.orderNo+"')\">已送达</button>";
                    }
                    return html;
                }
            },
            rowEvents: [
                {name:"同意",class:"btn-success",
                    click:function(row){
                       // auditFlow([row]);
                       alert('同意!');
                    }
                },
                {name:"拒绝",class:"btn-danger",
                    click:function(row){
                       // refuseFlow([row]);
                       alert('拒绝');
                    }
                }
            ],
            operationEvents: []
        }
    };

    $scope.dbFormGrid = {options: formGridOptions, events: formGridEvents};


}]);



app.controller('OrderDetailController', ['dbUtils','$scope','$modalInstance', '$state', 'OrderService','source', function (dbUtils,$scope,$modalInstance, $state, orderService,source) {
    orderService.getDetail(source.orderNo).then(function(response){
        if(response.code == 0){
            $scope.order = response.data;
            console.log(response.data);
        }
    });
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);